package com.reins.bookstore.entity;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Data;

import javax.persistence.*;
import java.math.BigDecimal;

@Data
@Entity
@Table(name = "orderitem")
@JsonIgnoreProperties(value = {"handler","hibernateLazyInitializer","fieldHandler"})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "orderlistId")
public class OrderItem {

    @Id
    @Column(name = "orderlist_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int orderlistId;
    @Column(name = "book_id")
    private int bookId;
    @Column(name = "book_num")
    private int bookNum;
    @Column(name = "order_id")
    private int orderId;
    @Column(name = "list_cost")
    private BigDecimal listCost;
//
    @JsonIgnoreProperties(value = {"orderItems"})
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = Orders.class)
    @JoinColumn(name = "order_id",referencedColumnName = "order_id",insertable = false,updatable = false)
    private Orders orders;

    @JsonIgnoreProperties(value = {"orderItems"})
    @ManyToOne(fetch = FetchType.LAZY,targetEntity = Book.class)
    @JoinColumn(name = "book_id",referencedColumnName = "id",insertable = false,updatable = false)
    private Book book;

    public OrderItem(int orderlistId, int bookId, int bookNum, int orderId, BigDecimal listCost){
        this.orderlistId = orderlistId;
        this.bookId = bookId;
        this.bookNum = bookNum;
        this.orderId = orderId;
        this.listCost = listCost;
    }
    public OrderItem(Integer o, Integer bookId, Integer bookNum, Integer orderId, BigDecimal listCost) {
        this.orderlistId = 0;
        this.bookId = bookId;
        this.bookNum = bookNum;
        this.orderId = orderId;
        this.listCost = listCost;
    }

    public OrderItem() {

    }
}
